package com.fenghen.demo01.test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class ThreadPoolExecutorExample {

    public static void main(String[] args) {

        long startTime = System.currentTimeMillis();
        ThreadPoolExecutor pool = new ThreadPoolExecutor(3, 5, 0L, java.util.concurrent.TimeUnit.MILLISECONDS, new java.util.concurrent.ArrayBlockingQueue<Runnable>(3));

        for (int i = 0; i < 10; i++) {
            final int taskId = i;
            pool.submit(() -> {
                long sum = 0;
                for (int j = 1; j <= 1000000000; j++) {
                    sum += j;
                }
                System.out.println("Thread " + taskId + " calculated sum: " + sum);
            });
        }

        pool.shutdown();
        
        while (!pool.isTerminated()) {
            // 等待所有任务完成
        }
        long endTime = System.currentTimeMillis();

        System.out.println("Normal Threads Execution Time: " + (endTime - startTime) + " ms");
    }
}